package br.com.andreikeda.utility.gestor.finance.storage.database.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import br.com.andreikeda.utility.gestor.finance.storage.database.model.Stock;
import br.com.andreikeda.utility.gestor.finance.utils.ColumnConstants;

/**
 * Created by LimaoGames on 22/01/2015.
 * contact me: andreikeda87@gmail.com
 */
public class StockDAO extends GenericDAO {

    private static StockDAO instance;

    private final String TABLE = "STOCK";
    private final String WHERE_CLAUSE_DEFAULT = "code=?";

    public static StockDAO getInstance() {
        if (instance == null)
            instance = new StockDAO();
        return instance;
    }

    @Override
    public boolean delete(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Stock))
            return false;
        return mLite.delete(TABLE, WHERE_CLAUSE_DEFAULT, new String[]{((Stock) object).getCode()}) > 0;
    }

    @Override
    public void deleteAll(SQLiteDatabase mLite, String whereClause, String[] whereArgs) {
        mLite.delete(TABLE, whereClause, whereArgs);
    }

    @Override
    public boolean insert(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Stock))
            return false;
        ContentValues cv = new ContentValues();
        cv.put(ColumnConstants.CODE, ((Stock) object).getCode());
        cv.put(ColumnConstants.TOTAL, ((Stock) object).getTotal());
        cv.put(ColumnConstants.COMPANY_ID, ((Stock) object).getCompanyId());
        mLite.insert(TABLE, null, cv);
        return true;
    }

    @Override
    public List list(SQLiteDatabase mLite) {
        Cursor c = mLite.query(TABLE, null, null, null, null, null, null);
        List stockes = new ArrayList<Stock>();
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                stockes.add(
                        new Stock(c.getString(c.getColumnIndex(ColumnConstants.CODE)),
                                c.getInt(c.getColumnIndex(ColumnConstants.TOTAL)),
                                c.getLong(c.getColumnIndex(ColumnConstants.COMPANY_ID))));
                c.moveToNext();
            }
        }
        return stockes;
    }

    @Override
    public List listFrom(SQLiteDatabase mLite, String whereClause, String[] whereArgs) {
        Cursor c = mLite.query(TABLE, null, whereClause, whereArgs, null, null, null);
        List stockes = new ArrayList<Stock>();
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                stockes.add(
                        new Stock(c.getString(c.getColumnIndex(ColumnConstants.CODE)),
                                c.getInt(c.getColumnIndex(ColumnConstants.TOTAL)),
                                c.getLong(c.getColumnIndex(ColumnConstants.COMPANY_ID))));
                c.moveToNext();
            }
        }
        return stockes;
    }

    @Override
    public boolean update(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Stock))
            return false;
        ContentValues cv = new ContentValues();
        cv.put(ColumnConstants.TOTAL, ((Stock) object).getTotal());
        mLite.update(TABLE, cv, WHERE_CLAUSE_DEFAULT, new String[]{((Stock) object).getCode()});
        return true;
    }

    @Override
    public void updateAll(SQLiteDatabase mLite, ContentValues cv, String whereClause, String[] whereArgs) {
        mLite.update(TABLE, cv, whereClause, whereArgs);
    }

    private StockDAO() {

    }
}
